package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.Classes;
import com.neusoft.neuiotms.dcom.model.QClasses;
import com.neusoft.neuiotms.dcom.repository.CustomizedClassesRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedClassesRepositoryImpl extends AbstractRepository implements CustomizedClassesRepository {

    @Override
    public List<Classes> index(final Searchable searchable) {
        final QClasses qClasses = QClasses.classes;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Classes> query = this.query().selectFrom(qClasses);
        query.where(where);
        return query.fetch();
    }

    @Override
    protected Class<?> getModelClass() {
        return Classes.class;
    }

    @Override
    public Page<Classes> search(final Pageable pageable, final Searchable searchable) {
        final QClasses qClasses = QClasses.classes;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Classes> query = this.query().selectFrom(qClasses);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QClasses qClasses = QClasses.classes;

        // 名称检索
        where.and(super.containsStr(qClasses.name, searchable, "name"));

        return where;
    }
}
